package com.jason.kotlin.banner

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.jason.kotlin.R
import com.jason.kotlin.model.BannerEntity

class BannerListAdapter(private val context: Context) :
    RecyclerView.Adapter<BannerListAdapter.BannerViewHolder>() {

    private var banners : MutableList<BannerEntity> = ArrayList();

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        BannerViewHolder(
            LayoutInflater.from(
                context
            ).inflate(R.layout.item_banner, parent, false)
        )

    override fun onBindViewHolder(holder: BannerViewHolder, position: Int) {
        Glide.with(context).load(banners.get(position).imagePath).into(holder.image)
    }

    override fun getItemCount() = banners.size

    fun setBanners(list: MutableList<BannerEntity>) {
        list?.let {
            banners.clear()
            banners.addAll(list)
            notifyDataSetChanged()
        }
    }



    class BannerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val image:ImageView = itemView.findViewById(R.id.image)
    }

}

